<?php

class Ext_Music_Show
{
    public $photo;
    public $flash;
    public $siteName;
    public $siteViewUrl;

    public function __construct()
    {
        $this->photo = new Ext_Image_Show();
    }
    
    public function build($musicId)
    {
        $musicRow = App_Model_DbTable_Music::getInstance()->getRowById($musicId);
        if(!$musicRow){
            return "";
        }
        
        $imageId = $musicRow['imageId'];
        $host    = $musicRow['host'];

        $this->photo->build($imageId);
        
        $config = Ext_Config::getInstance()->getConfig("attachment");
        $attachmentConfig = $config->toArray();
        if(!is_array($attachmentConfig)){
            $attachmentConfig = array();
        }
        if(!is_array($attachmentConfig['music'])){
            $attachmentConfig['music'] = array();
        }
        $hostAdapter = '';
        foreach($attachmentConfig['music'] as $hostKey=>$hostVal)
        {
            if(!isset($hostVal['host'])){
                continue;
            }
            $hostDomain = $hostVal['host'];
            if(preg_match("/".$hostDomain."$/si", $host)){
                $hostAdapter = $hostKey;
                break;
            }
        }
        if(!$hostAdapter)
        {
            require_once 'Ext/Music/Exception.php';
            throw new Ext_Music_Exception("Music show adapter '".$host."' not found.");
        }
        
        $classname = "Ext_Music_Show_Adapter_".ucfirst($hostAdapter);
        if(!class_exists( $classname ))
        {
            require_once 'Ext/Music/Exception.php';
            throw new Ext_Music_Exception("Music show adapter '".$hostAdapter."' not found.");
        }

        $adapter = new $classname($musicId);
        
        //$this->player   = $adapter->player;
        $this->flash       = $adapter->flash;
        $this->siteName    = $adapter->siteName;
        $this->siteViewUrl = $adapter->siteViewUrl;
    }
}
